package com.wise.site.main;

import com.wise.site.R;
import com.wise.site.adapter.BusinessAdapter;
import com.wise.site.adapter.BusinessAdapter.BUSINESS_TYPE;
import com.wise.site.adapter.InfoListAdatper;
import com.wise.site.adapter.InfoListAdatper.ITEM_TYPE;
import com.wise.site.adapter.KeywordAdapter;
import com.wise.site.database.SearKeyDB;
import com.wise.site.detail.BuyDetailsActivity;
import com.wise.site.detail.ShopsDetailsActivity;
import com.wise.site.detail.SupplyDetailsActivity;
import com.wise.site.protocol.base.SoapItem;
import com.wise.site.utils.Constanct;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class SearchActivity extends Activity implements View.OnClickListener,AdapterView.OnItemClickListener {
	private Builder builder;
	private Button multButton;
	private int searchType;
	private ListView mListView;
	private ListView mKeyListView;
	private InfoListAdatper mShopAdatper;
	private BusinessAdapter mProductAdatper;
	private BusinessAdapter mBuyAdatper;
	private KeywordAdapter mKeywordAdapter;
	private EditText mKeyword;
	private boolean isKeywordClicked = false;
	
	private OnClickListener onSelector = new OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			multButton.setText(getResources().getStringArray(
					R.array.search_type_array)[which]);
			searchType = which;
			dialog.dismiss();
//			mProductAdatper.removeAll();
//			mShopAdatper.removeAll();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.search_activity);
		
		Intent intent = getIntent();
		searchType = intent.getIntExtra(Constanct.INTENT_SEARCH_TYPE, 0);

		((TextView) findViewById(R.id.activity_title))
				.setText(R.string.search_title);
		multButton = (Button) findViewById(R.id.mult_button);
		multButton.setText(getResources().getStringArray(
				R.array.search_type_array)[searchType]);
		multButton.setOnClickListener(this);
		
		findViewById(R.id.search_button).setOnClickListener(this);
		findViewById(R.id.title_back).setOnClickListener(this);
		
		mKeyword = (EditText)findViewById(R.id.keyword);
		mKeyword.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) { 
				if(s == null || s.length() == 0 || isKeywordClicked){
					isKeywordClicked = false;
					return;
				}
				isKeywordClicked = false;
				if(mKeyListView.getVisibility() != View.VISIBLE){
					mListView.setVisibility(View.GONE);
					mKeyListView.setVisibility(View.VISIBLE);
				}
				
				mKeywordAdapter.setList(SearKeyDB.getLikeKey(s.toString()));
			}
		});
		
		mListView = (ListView) findViewById(R.id.list);
		mKeyListView = (ListView) findViewById(R.id.keys_list);
		mShopAdatper = new InfoListAdatper(ITEM_TYPE.SHOP_TYPE, this, "searchs");
		mProductAdatper = new BusinessAdapter(BUSINESS_TYPE.SALE_TYPE, this, "searchs");
		mBuyAdatper = new BusinessAdapter(BUSINESS_TYPE.BUY_TYPE, this, "searchs");
		mProductAdatper.setListView(mListView);
		mKeywordAdapter = new KeywordAdapter(this);
		mKeywordAdapter.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(v.getId() == R.id.tab_del){;
					if(v.getTag() instanceof String){
						SearKeyDB.removeKey((String) v.getTag());
						mKeywordAdapter.setList(SearKeyDB.getLikeKey(mKeyword.getText().toString()));
					}
				} else {
					isKeywordClicked = true;
					String text = ((TextView)v.findViewById(R.id.tab_tx)).getText().toString();
					mKeyword.setText(text);
					loadData();
				}
			}
		});
		mKeyListView.setAdapter(mKeywordAdapter);
		
		mListView.setOnItemClickListener(this);

		initDialog();

	}

	private void initDialog() {
		builder = new AlertDialog.Builder(this);
		
		builder.setSingleChoiceItems(R.array.search_type_array, searchType, onSelector);

//		builder.setNegativeButton(getString(R.string.sure),
//				new OnClickListener() {
//					public void onClick(DialogInterface dialog, int which) {
//						dialog.dismiss();
//					}
//				});
	}
	
	private void loadData() {
		if(mListView.getVisibility() != View.VISIBLE){
			mKeyListView.setVisibility(View.GONE);
			mListView.setVisibility(View.VISIBLE);
		}
		Editable keyword = mKeyword.getText();
		if(keyword != null && keyword.length() > 0){
			mProductAdatper.removeAll();
			mShopAdatper.removeAll();
			mBuyAdatper.removeAll();
			SearKeyDB.saveKey(keyword.toString());
			if(searchType == 0){
	//			mProductAdatper.setType(BUSINESS_TYPE.SALE_TYPE);
				mProductAdatper.setListView(mListView);
				mProductAdatper.moveToFristPage();
				mProductAdatper.setKeyword(keyword.toString());
				mProductAdatper.setSearchType(1);
				mProductAdatper.loadData(false);
			} else if(searchType == 1) {
				mShopAdatper.setListView(mListView);
				mShopAdatper.moveToFristPage();
				mShopAdatper.setKeyword(keyword.toString());
				mShopAdatper.setType(3);
				mShopAdatper.loadData();
			} else if(searchType == 2){
	//			mProductAdatper.setType(BUSINESS_TYPE.BUY_TYPE);
				mBuyAdatper.setListView(mListView);
				mBuyAdatper.moveToFristPage();
				mBuyAdatper.setKeyword(keyword.toString());
				mBuyAdatper.setSearchType(2);
				mBuyAdatper.loadData(false);
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.mult_button: {
			builder.setSingleChoiceItems(R.array.search_type_array, searchType, onSelector);
			builder.show();
			break;
		}
		case R.id.search_button: {
			loadData();
			((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
			break;
		}
		case R.id.title_back: {
			finish();
			break;
		}
		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent();
		switch(searchType){
		case 0:
			intent.setClass(this, SupplyDetailsActivity.class);
			break;
		case 1:
			intent.setClass(this, ShopsDetailsActivity.class);
			break;
		case 2:
			intent.setClass(this, BuyDetailsActivity.class);
			break;
		}
		intent.putExtra(Constanct.INFO_ENTRY, (SoapItem)arg1.getTag());
		startActivity(intent);
	}

}
